﻿using JaxDug.DataAccess;
using JaxDug.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace JaxDug.Areas.CodeCamp.Controllers
{
    public partial class PresentationController : Controller
    {
        //
        // GET: /CodeCamp/Presentation/
        [AllowAnonymous]
        public virtual ActionResult Index()
        {
            try
            {
                var db = new JaxdugEntities();
                var all=db.Presentations.ToArray();
                
                return View(all);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                
            }
            return View();
        }

        public virtual ActionResult Submit()
        {
            JaxDug.Areas.CodeCamp.Models.Presentation viewModel = new JaxDug.Areas.CodeCamp.Models.Presentation();
            ViewBag.WasSubmitted = false;
            return View(viewModel);
        }

        [HttpPost]
        public virtual ActionResult Submit(JaxDug.Areas.CodeCamp.Models.Presentation presModel)
        {
            MembershipUser myObject = Membership.GetUser();
            Guid UserID = (Guid)myObject.ProviderUserKey;
            ViewBag.WasSubmitted = true;
            JaxdugEntities db = new JaxdugEntities();
            Presentation model = new Presentation();
            model.Name = presModel.Name;
            model.Description = presModel.Description;
            model.Speaker = db.Speakers.Where<Speaker>(s => s.UserID == UserID).FirstOrDefault();
            model.DateCreated = DateTime.Now;
            db.Presentations.AddObject(model);
            db.SaveChanges();
            ModelState.Clear();
            return View("Submit", presModel);
        }

    }
}
